package com.lingx.service;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.lingx.bean.ContextBean;
import com.lingx.bean.TokenBean;
import com.lingx.bean.UserBean;
import com.lingx.model.IContext;
import com.lingx.model.IExecutor;
import com.lingx.model.IPerformer;
import com.lingx.model.Page;
import com.lingx.model.impl.DefaultEntity;
import com.lingx.model.impl.DefaultMethod;
import com.lingx.utils.TokenUtils;
import com.lingx.utils.Utils;
import com.lingx.web.api.IApi;

@Component("lingxProcessEngineService")
public class ProcessEngineService {
	@Resource
	private LingxService lingxService;
	@Resource
	private MethodService methodService;
	@Resource
	private ModelService modelService;
	@Resource
	private InterpretService interpretService;
	@Resource
	private ValidateService validateService;
	@Resource
	private UserService userService;
	@Resource
	private IPerformer performer;

	public Map<String, Object> process(Map<String, Object> params) throws Exception {
		Map<String, Object> ret = IApi.getRetMap(1, "未处理");
		DefaultEntity entity = null;
		DefaultMethod method = null;
		boolean isValidator = false;
		long stime=System.currentTimeMillis();
		String ecode=IApi.getParamString(params, "e", "");//params.get("e").toString();
		String mcode=IApi.getParamString(params, "m", "");//params.get("m").toString()
		if(Utils.isNull(ecode))ecode=IApi.getParamString(params, "ecode", "");
		if(Utils.isNull(mcode))mcode=IApi.getParamString(params, "mcode", "");

		TokenBean token=TokenUtils.getTokenData(params.get("lingxtoken").toString());
		if(!this.userService.isAuthFunc(token.getUserId(), ecode, mcode)) {
			ret.put("code", -1);
			ret.put("message","无此功能权限");
			return ret;
		}
		
		entity =this.modelService.getEntityByCache(ecode); //this.modelService.getEntity(ecode);
		method = this.methodService.getMethod(mcode, entity);

		String ip=IApi.getParamString(params, "X-Real-IP", "");
		
		if (!method.getEnabled()) {
			ret.put("code", -1);
			ret.put("message","无此功能权限");
			return ret;
		}
		UserBean userBean =new UserBean();
		userBean.setId(token.getUserId());
		userBean.setOrgId(token.getOrgId());
		IContext context = new ContextBean(userBean,entity,method);

		if(method.getFields()!=null) {
			this.interpretService.inputFormat(params, method.getFields().getList(), entity);
			this.performer.initField(method.getFields().getList());
		}
		//System.out.println(JSON.toJSONString(params));
		performer.addParam(params);
		performer.addParam("ENTITY_CODE", entity.getCode());
		performer.addParam("CONTEXT", context);
		performer.addParam("REQUEST", params);
		isValidator = this.validateService.validator(params,method, context, performer, ret);
		if (isValidator) {
			for (IExecutor exe : method.getExecutors().getList()) {
				if(exe==null)continue;
				performer.addParam("ExeRet", ret);
				Object obj = exe.execute(params, context, performer);
				if (obj instanceof Map) {
					ret = (Map) obj;
				} else if (obj instanceof String) {
					ret.put("message", obj);
				} else {
					ret.put("data", obj);
				}
			}
		}


		this.lingxService.logOperate(method.getCode(),token.getUserId(), entity.getName()+"-"+method.getName(), ip,params, JSON.toJSONString(ret),System.currentTimeMillis()-stime);
		userBean=null;
		context=null;
		return ret;
	}

}
